package edu.upenn.cis542;

import android.R.layout;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	private static final int READY_DIALOG = 1;
	private static final int CORRECT_DIALOG = 2;
	private static final int INCORRECT_DIALOG = 3;
	
	EditText editText;
	TextView typeText;
	long start, end, time, bestTime=Long.MAX_VALUE;
	int bestFlag;
	
	@SuppressWarnings("all")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		showDialog(READY_DIALOG);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	@SuppressWarnings("all")
	public void compareStringInput(View view)
	{
		Context context = getApplicationContext();
		CharSequence text;
		int duration = Toast.LENGTH_SHORT;

		editText = (EditText)findViewById(R.id.editText1);
		typeText = (TextView)findViewById(R.id.textView1);
		
	//	Toast toast;
		String original = typeText.getText().toString();
		String entered = editText.getText().toString();
		end = System.currentTimeMillis();
		time = ((end-start)/1000);
		if(bestTime > time)
		{
			bestTime = time;
			bestFlag = 1;
		}
		else
			bestFlag = 0;
		if(original.equals(entered))
		{
			
		//	text = "Hurray! You entered it right! You took "+time+" seconds";
			removeDialog(CORRECT_DIALOG);
			showDialog(CORRECT_DIALOG);
			
		//	toast = Toast.makeText(context, text, duration);
		//	toast.setGravity(Gravity.BOTTOM|Gravity.CENTER, 0, 60);
		//	toast.show();
		}
		else
		{
			
		//	text = "Bam! You got it wrong!";
			removeDialog(INCORRECT_DIALOG);
			showDialog(INCORRECT_DIALOG);
		//	toast = Toast.makeText(context, text, duration);
		//	toast.setGravity(Gravity.BOTTOM|Gravity.CENTER, 0, 60);
		//	toast.show();
		}
	}
	
   @SuppressWarnings("all")	
   protected Dialog onCreateDialog(int id) 
   {
	    	if (id == READY_DIALOG) 
	    	{
		    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
	                // this is the message to display
		    	builder.setMessage(R.string.ready); 
	                // this is the button to display
		    	builder.setPositiveButton(R.string.yes,
		    		new DialogInterface.OnClickListener() {
	                           // this is the method to call when the button is clicked 
		    	           public void onClick(DialogInterface dialog, int id) {
	                                   // this will hide the dialog
		    	        	   start = System.currentTimeMillis();
		    	        	   dialog.cancel();
		    	           }
		    	         });
	    		return builder.create();
	    	}
	    	else if(id == CORRECT_DIALOG)
	    	{
	    		AlertDialog.Builder builder = new AlertDialog.Builder(this);
	    		String ifCorrectMessage=null;
	    		if(bestFlag == 0)
	    		{
	    			 ifCorrectMessage = "Hurray! You entered it right! You took "+time+" seconds. The best time was "+bestTime+". Click OK to play again";
	    		}
	    		else if(bestFlag == 1)
	    		{
	    			ifCorrectMessage = "Hurray! You entered it right! You took "+time+" seconds. It is the best time. Click OK to play again";
	    		}
                // this is the message to display
	    		builder.setMessage(ifCorrectMessage); 
                // this is the button to display
	    		builder.setPositiveButton(R.string.ok,
	    		new DialogInterface.OnClickListener() {
                           // this is the method to call when the button is clicked 
	    	           public void onClick(DialogInterface dialog, int id) {
                                   // this will hide the dialog
	    	        	   dialog.cancel();
	    	        	   start = 0;
	    	        	   end = 0;
	    	        	   time = 0;
	    	        	   editText.setText("");
	    	        	   showDialog(READY_DIALOG);
	    	           }
	    	         });
    		return builder.create();
	    	}
	    	else if(id == INCORRECT_DIALOG)
	    	{
	    		AlertDialog.Builder builder = new AlertDialog.Builder(this);
                String ifIncorrectMessage = "Bam! You got it wrong! Click OK to play again"; 
	    		// this is the message to display
	    		builder.setMessage(ifIncorrectMessage); 
                // this is the button to display
	    		builder.setPositiveButton(R.string.ok,
	    		new DialogInterface.OnClickListener() {
                           // this is the method to call when the button is clicked 
	    	           public void onClick(DialogInterface dialog, int id) {
                                   // this will hide the dialog
	    	        	   dialog.cancel();
	    	        	   start = 0;
	    	        	   end = 0;
	    	        	   time = 0;
	    	        	   editText.setText("");
	    	        	   showDialog(READY_DIALOG);
	    	           }
	    	         });
    		return builder.create();
	    	}
	    	else return null;
   }
	
	
	public void quitApplication(View v)
	{
		finish();
	}
}
